package com.cfushn.ddd.application.dto.res;

import com.cfushn.ddd.application.dto.CodeEnum;
import com.cfushn.ddd.application.dto.DTO;
import java.util.Collection;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * PageResultDTO
 *
 * @author CFuShn 2022/6/29 11:56
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("all")
public class PageResDTO<T> extends DTO {

    private static final long serialVersionUID = 1L;

    public static final PageResDTO<?> ERROR = new PageResDTO<>(CodeEnum.ERROR.getCode(),
        null, null, null, null, "error!");

    protected String code;

    protected Collection<T> data;

    /** 总条数 */
    private Long total;

    /** 页大小 (非必须) */
    private Long pageSize;

    /** 总页数 (非必须) */
    private Long totalPage;

    protected String msg = "";

    public static <R> PageResDTO<R> succeed(Collection<R> data, long total) {
        return new PageResDTO<>(CodeEnum.SUCCESS.getCode(), data, total, null, null, "success!");
    }

    public static <R> PageResDTO<R> succeed(Collection<R> data, long total, long pageSize) {
        long totalPage = total == 0 ? 0 : (long) Math.ceil((double) total / pageSize);
        return new PageResDTO<>(CodeEnum.SUCCESS.getCode(), data, total, pageSize, totalPage,
            "success!");
    }

    public static <T> PageResDTO<T> failed(String msg) {
        return new PageResDTO<>(CodeEnum.ERROR.getCode(), null, null, null, null, msg);
    }

    public static <T> PageResDTO<T> failed() {
        return failed("error!");
    }

}
